using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Core.Toolkit.Core.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class ZY_JSAService : BaseAppService<ZY_JSADto, ZY_JSA, ZY_JSARepository>
    {
        readonly ExcelHelp _excelHelp;
        readonly UserIdentity _userIdentity;
        public ZY_JSAService(ZY_JSARepository repository, ExcelHelp excelHelp, IHttpContextAccessor accessor) : base(repository)
        {
            _excelHelp = excelHelp;
            _userIdentity = accessor.HttpContext.User.GetUserIdentity();
        }



        private LoadPageOffsetModel<ZY_JSA, ZY_JSADto>
            GetLoadPageOffsetMode(PagingModel<ZY_JSADto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModel(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            var dtoModel = pagingQueryModel.Model;
            var filterList = this.CreateQRList()
            .Add("ApplicationNumber", "in"); //编号
            if (_userIdentity.Headoffice)
                filterList.Add("CreateFactory", "="); //总部从查询条件取工厂DI
            else
                filterList.Add("CreateFactory", "=", _userIdentity.Factory);
            var filterExpre = filterList.BuildeQR(dtoModel.ToEntity<ZY_JSA>());
            loadPageOffsetModel.QueryWhere = filterExpre;
            //.Add("OperatingUnit","=") //作业单位
            //.Add("JobManagere","=") //作业负责人
            //.Add("WorkDescription","=") //工作描述
            //.Add("PersonalProtectiveEquipment","=") //个人防护用品
            //.Add("ParticipateJSPAnalysts","=") //未参与分析人员
            //.Add("RiskIdentificationSafety","=") //其它安全措施
            //.Add("EstimateBeginTime","=") //预计开始时间
            //.Add("JobStartTime","=") //有效开始时间
            //.Add("JobEndTime","=") //有效结束时间
            //.Add("State","=") //状态
            //.Add("CreateFactory","=") //隶属工厂
            //.Add("CreateDept","=") //添加部门
            //.Add("UpdateDept","=") ;//更改部门


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new ZY_JSA()
            {
                Id = model.Id,
                ApplicationNumber = model.ApplicationNumber,//编号
                OperatingUnit = model.OperatingUnit,//作业单位
                JobManagere = model.JobManagere,//作业负责人
                WorkDescription = model.WorkDescription,//工作描述
                PersonalProtectiveEquipment = model.PersonalProtectiveEquipment,//个人防护用品
                ParticipateJSPAnalysts = model.ParticipateJSPAnalysts,//未参与分析人员
                RiskIdentificationSafety = model.RiskIdentificationSafety,//其它安全措施
                EstimateBeginTime = model.EstimateBeginTime,//预计开始时间
                JobStartTime = model.JobStartTime,//有效开始时间
                JobEndTime = model.JobEndTime,//有效结束时间
                State = model.State,//状态
                CreateFactory = model.CreateFactory,//隶属工厂
                CreateDept = model.CreateDept,//添加部门
                UpdateDept = model.UpdateDept,//更改部门
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<ZY_JSADto>> GetList(PagingModel<ZY_JSADto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<ZY_JSADto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.ApplicationNumber,
                ApplicationNumber_title = "编号",
                model.OperatingUnit,
                OperatingUnit_title = "作业单位",
                model.JobManagere,
                JobManagere_title = "作业负责人",
                model.WorkDescription,
                WorkDescription_title = "工作描述",
                model.PersonalProtectiveEquipment,
                PersonalProtectiveEquipment_title = "个人防护用品",
                model.ParticipateJSPAnalysts,
                ParticipateJSPAnalysts_title = "未参与分析人员",
                model.RiskIdentificationSafety,
                RiskIdentificationSafety_title = "其它安全措施",
                model.EstimateBeginTime,
                EstimateBeginTime_title = "预计开始时间",
                model.JobStartTime,
                JobStartTime_title = "有效开始时间",
                model.JobEndTime,
                JobEndTime_title = "有效结束时间",
                model.State,
                State_title = "状态",
                model.CreateFactory,
                CreateFactory_title = "隶属工厂",
                model.CreateDept,
                CreateDept_title = "添加部门",
                model.UpdateDept,
                UpdateDept_title = "更改部门",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
